An array is a collection of elements organized in a rectangle grid that spans across multiple dimensions. You can read a value from an array or assign a value to an element. It is also possible to read values from, or to assign values to, a set of elements. For examples, replacing a row of a matrix with another row, and deleting a matrix from a 3D array.
This indexing method is easy to understand, but it can be somewhat inefficient and inflexible. If you are working with large arrays, we recommend using linear indexing whenever it is feasible.
Let us consider an illustrative example above. It shows the array A with ndims(A) equal to 3. You may consider A as stacking three A had ndims(A) == m, then an element should be addressed by an

For example, the element in the 1st row and 1st column of the 1st matrix is addressed by A(1,1,1). The element in the 3rd row of the 4th column of the 3rd matrix is address by A(3,4,3). In the code segment below, we first create a 3D array called A, and then read the elements A(1,1,1) and A(3,4,3):
matrix1 = [1 4 7 10 13; 16 19 22 25 28; 31 34 37 40 43];
matrix2 = [2 5 8 11 14; 17 20 23 26 29; 32 35 38 41 44];
matrix3 = [3 6 9 12 15; 18 21 24 27 30; 33 36 39 42 45];
A(:,:,1) = matrix1
A(:,:,2) = matrix2
A(:,:,3) = matrix3
% Accessing elements
A(1,1,1)
A(3,4,3)
ans =
1
ans =
42
You can replace the values of A(1,1,1) and A(3,4,3) using the code below:
A(1,1,1) = 999
A(3,4,3) = 888
To access multiple elements, use a vector of subscripts. For example, A([1,2], 3, 2) gives elements in the 1st and 2nd rows of the 3rd column in the 2nd matrix.
When a subscript is not given explicitly, it is assumed 1. For examples,
A(1,2) means A(1,2,1),A([1 2], [2, 3]) means A([1 2], [2 3], 1).When any of the given subscript is empty, an empty array is returned. For examples,
A([], 1) returns [],A([]) returns [].Linear indexing is more efficient than subscript indexing, since it does not require converting a subscript to the corresponding memory address at which numbers are stored. Such a conversion requires potentially time consuming operations.
The example below illustrates how linear indexing works. An element of the array A is addressed by a single integer, called a linear index. The index (1,1,1), as seen from the top-left corner of A(:,:,1) below. When (2,1,1) in the next row. When it finishes all rows in a column, it moves to the next column. The arrows in the figure below show the elements corrsponding to the linear index as it is incremented. There are in total 45 elements in the array A. Hence, (3,5,3).

It is possible to access multiple elements by using multiple indexes, such as A([1 3 4]). In general, in A(ind), if ind is a vector and A is column (row) vector, the output array is a column (row) vector. Otherwise, the output array should always have the same size as the index array.
end and :The discussion below refers to A given by the following figure.

When a subscript is not given explicitly, it is assumed 1. For examples,
A(1,2) means A(1,2,1),A([1 2], [2, 3]) means A([1 2], [2 3], 1).When any of the given subscript is empty, an empty array is returned. For examples,
A([], 1) returns [],A([]) returns [].Use end to access the last element in a dimension. Actually, when end is used to index an array, it has the value equal to the dimension length. For example, end in A(end, 1, 1) is equal to 3. You can do arithmetic using end, such as end - 1 and end / 3. The operator end should not be used in an empty array. Otherwise, an error message will be thrown. See below for more examples:
A(end, 1) returns A(3, 1)A(end - 2, 1) returns A(1, 1)A(end, 1, end - 2) returns A(3, 1, 1)If it is desirable to access all elements in the column, we can use A(:, 3, 2). The operator : means "all elements" the along the dimension. Similarly, to access the elements in the 2nd matrix, we can use A(:, :, 2). To access all elements in A, we may use A(:,:,:).
Subscript will be converted to integer, if possible. For example, A(1,'a') is the same as A(1, 97).
Subscript should be positive integer, equal to neither NaN nor Inf.